<template>
<!--印章档案管理》新建印章表单-->
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-divider>基本信息</a-divider>
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createBy">
<!--                  <select-person-input-->
<!--                    placeholder="请选择经办人"-->
<!--                    :disabled="formDisabled"-->
<!--                    v-model="model.createBy" disabled/>-->
                  <!-- <select-user-by-dep :multi="false" v-model="model.createBy" disabled/> -->
                  <j-select-user-by-dep :multi="false" v-model="model.createBy" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="handlerDeptId">
                  <j-select-depart v-model="model.handlerDeptId" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
                  <j-date placeholder="请选择申请时间" v-model="model.createTime" :show-time="true"
                          date-format="YYYY-MM-DD HH:mm" style="width: 100%" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="印章编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sealQualificationId">
                  <a-input v-model="model.sealQualificationId" placeholder="请输入印章编号" disabled></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="印章所属公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sealCompany">
<!--                  <j-select-company :disabled=formDisabled @input="changeName2"-->
<!--                                    v-model="model.sealCompany"-->
<!--                                    placeholder="请选择印章所属公司" />-->
                  <select-Company-input :disabled="formDisabled" @back="changeName2" v-model="model.sealCompany"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="印章类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sealType">
                  <dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    :sealName.sync="sealName"
                    v-model="model.sealType"
                    dictCode="yzlx_type"
                    @input="changeName1"
                    placeholder="请选择印章类别"
                  />
                </a-form-model-item>
              </a-col>
          <a-col :span="8">
            <a-form-model-item label="印章ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="esealId">
              <a-input :disabled="formDisabled" v-model="model.esealId" placeholder="请输入印章ID"/>
            </a-form-model-item>
          </a-col>
              <a-col :span="8">
                <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
                  <a-input v-model="model.name" placeholder="请输入名称" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="印章用途" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sealPurpose">
                  <!-- <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.sealPurpose" dictCode="seal_purpose" placeholder="请选择印章用途"/> -->
                  <a-input :disabled="formDisabled" v-model="model.sealPurpose" placeholder="请输入印章用途" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="印章负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sealCertificationHead">
<!--                  <select-person-input placeholder="请选择印章负责人" :disabled="formDisabled" v-model="model.sealCertificationHead"/>-->
                  <select-user-by-dep placeholder="请选择印章负责人" :disabled="formDisabled" :multi="false" v-model="model.sealCertificationHead"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="印章管理员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="handlerId">
                  <select-user-by-dep placeholder="请选择印章管理员" :disabled="formDisabled" :multi="false" v-model="model.handlerId"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="授权责任人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="authorizedPerson">
<!--                  <select-person-input-->
<!--                      :disabled=formDisabled-->
<!--                      v-model="model.authorizedPerson" disabled/>-->
                  <!-- <select-user-by-dep :disabled="formDisabled" :multi="false" v-model="model.authorizedPerson"/> -->
                  <select-user-by-dep :disabled="true" :multi="false" v-model="model.authorizedPerson"/>
                </a-form-model-item>
              </a-col>
            </a-row>

            <a-divider>委托信息</a-divider>
            <a-row :gutter="48">
              <a-col :span="12">
                <a-form-model-item label="委托方" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delegate">
                  <!-- <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.delegate" dictCode="erp_company_files,file_name,id" placeholder="请选择委托方"/> -->
                  <j-select-depart disabled type="list" v-model="model.delegate" dictCode="" placeholder="请选择委托方"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="被委托方（印章保管人）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="trustee">
<!--                  <select-person-input placeholder="请选择被委托方（印章保管人）" disabled v-model="model.trustee"/>-->
                  <select-user-by-dep placeholder="请选择被委托方（印章保管人）" :multi="false" v-model="model.trustee" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="委托起止日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="entrustDateStart">
                  <a-date-picker disabled placeholder="请选择授权印章开始时间" :showTime="true" @change="handleChange" v-model="model.entrustDateStart" valueFormat="YYYY-MM-DD HH:mm"  style="width: 48%" />
                  <span class="fontDiy" style="width: 4px">至</span>
                  <a-date-picker 
                    disabled
                    :disabled-date="disabledDate"
                    :default-picker-value="this.model.entrustDateStart"
                    placeholder="请选择授权印章结束时间"
                    v-model="model.entrustDateEnd"
                    :showTime="true"
                    valueFormat="YYYY-MM-DD HH:mm"
                    style="width: 48%"
                  />

                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item
                  label="印章使用范围"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="sealCertificationUseScope"
                >
                  <a-textarea disabled :max-length="500" v-model="model.sealCertificationUseScope" rows="4" placeholder="请输入印章使用范围"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="注意事项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attentionMatters">
                  <a-textarea :disabled="formDisabled" :max-length="500" v-model="model.attentionMatters" rows="4" placeholder="请输入注意事项"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="上传附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="upload">
                  <j-upload :disabled="formDisabled" v-model="model.upload"></j-upload>
                </a-form-model-item>
              </a-col>
            </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { getAction,httpAction } from '@/api/manage'
import JDictNameTag from '@views/erp/administration/seal/modules/JDictNameTag.vue'
import DictSelectTag from '@views/erp/administration/seal/modules/DictSelectTag.vue'
import { mapGetters } from 'vuex';
import moment from 'moment';
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'

export default {
  name: 'ErpSealCertificateFileManageFormYz',
  components: {
    JDictNameTag,
    SelectUserByDep,
    DictSelectTag,
    SelectCompanyInput
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      activeKey: ['1','2','3'],
      sealName: '',
      companyName: '',
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        departmentId: [
          { required: true, message: '请选择经办部门!' },
        ],
        sealType: [
          { required: true, message: '请选择印章类别!' },
        ],
        // esealId: [
        //   { required: true, message: '请输入印章ID!' },
        // ],
        sealCompany: [
          { required: true, message: '请选择印章所属公司!' },
        ],
        handlerId: [
          { required: true, message: '请选择印章管理员!' },
        ],
        sealCertificationHead: [
          { required: true, message: '请选择印章负责人!' },
        ],
        sealPurpose: [
          { required: true, message: '请输入印章用途!' },
        ],
      },
      url: {
        add: '/api/erp/administration/seal/erpSealCertificateFileManage/add',
        edit: '/api/erp/administration/seal/erpSealCertificateFileManage/edit',
        queryById: '/api/erp/administration/seal/erpSealCertificateFileManage/queryById',
        generatedFormNumber: '/api/erp/base/generatedFormNumber'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    ...mapGetters(['userInfo']),
    moment,
    changeName1(val,name){
      this.model.sealType_dictText = name
      this.model.name = (this.model.sealCompany_dictText == undefined ? '' : this.model.sealCompany_dictText) + this.model.sealType_dictText
    },
    // 自动生成印章编号
    generatedFormNumber() {
      getAction(this.url.generatedFormNumber, { prefix: 'YZ', subSystemCode: '04' }).then(res => {
        if (res.success) {
          this.$set(this.model, 'sealQualificationId', res.result)
        }
      })
    }, 
    changeName2(val){
      if (val.length > 0) {
        this.model.sealCompany_dictText = val[0].fileName
      } else {
        this.model.sealCompany_dictText = ''
      }
      this.model.name = this.model.sealCompany_dictText + (this.model.sealType_dictText == undefined ? '' : this.model.sealType_dictText)
    },
    handleChange(value) {
      if (this.model.entrustDateStart) {
        if ((this.model.entrustDateEnd).diff((this.model.entrustDateStart), 'days') < 0) {
          this.model.entrustDateEnd = this.model.entrustDateStart
        }
      }
    },
    disabledDate(current) {
      return current < moment(this.model.entrustDateStart).subtract('day')
    },
    sealCompanyChange(value) {
      // eslint-disable-next-line eqeqeq
      this.model.name = this.companyName + this.sealName
    },
    sealTypeChange(value) {
      this.model.name = this.companyName + this.sealName
    },
    async add() {
      this.edit(this.modelDefault);
      this.userName = this.userInfo().empName;//申请人名称
      this.model.handlerDeptId = this.userInfo().deptId; //申请部门
      this.model.createTime = moment().format('YYYY-MM-DD HH:mm');
      this.generatedFormNumber();
      this.model.createBy = this.userInfo().username; //申请人
    },
    edit(record) {
      this.$refs.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
              that.$emit('unButtonDisable')
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }else{
          that.$emit('unButtonDisable')
        }

      })
    },
  }
}
</script>
